package cao.oschina.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import cao.oschina.R;
import hd.util.widget.listview.BasePushDownListView;

/**
 * 下拉刷新
 * <p/>
 * Created by Haidy on 15/3/12.
 */
public class PushDownListView extends BasePushDownListView implements BasePushDownListView.OnRefreshListener {

    private final String PUSH_DOWN_REFRESH = "下拉刷新";
    private final String REFRESHING = "正在刷新";
    private final String REFRESH_FINISH = "刷新完成";
    private final String LOOSEN_REFRESH = "释放刷新";

    private View headView;
    private RefreshView refreshView;
    private TextView refreshMessageView;

    private OnRefreshListener onRefreshListener;

    public PushDownListView(Context context) {
        this(context, null);
    }

    public PushDownListView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PushDownListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        headView = View.inflate(context, R.layout.refresh_view, null);
        setRefreshView(headView);
        refreshView = (RefreshView) headView.findViewById(R.id.refresh_view);
        refreshMessageView = (TextView) headView.findViewById(R.id.refresh_message);
        setOnRefreshListener(this);
    }


    @Override
    public void onStartRefresh() {
        refreshView.reset();
        refreshMessageView.setText(PUSH_DOWN_REFRESH);
    }

    @Override
    public void onScrolling(int offset, int headViewHeight) {
    }

    @Override
    public void onTrigger() {
        refreshMessageView.setText(LOOSEN_REFRESH);
    }

    @Override
    public void onUnTrigger() {
        refreshMessageView.setText(PUSH_DOWN_REFRESH);
    }

    @Override
    public void onRefresh() {
        refreshMessageView.setText(REFRESHING);
        refreshView.startRotation();
        if (onRefreshListener != null) {
            onRefreshListener.onRefresh();
        }
    }

    @Override
    public void overHeadScroll() {
        super.overHeadScroll();
        refreshView.stopRotation();
    }

    /**
     * 设置Refresh Message
     *
     * @param message the message to be displayed. 要被显示的信息
     */
    public void setRefreshMessage(String message) {
        refreshMessageView.setText(message);
    }

    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        this.onRefreshListener = onRefreshListener;
    }

    public interface OnRefreshListener {
        void onRefresh();
    }
}
